package com.zlt.bkparking.service.impl;

import com.zlt.bkparking.dto.ParkingDto;
import com.zlt.bkparking.dto.ParkingPriceDto;
import com.zlt.bkparking.entity.*;
import com.zlt.bkparking.mapper.BackUserMapper;
import com.zlt.bkparking.mapper.CarAddressMapper;
import com.zlt.bkparking.mapper.MyMapper;
import com.zlt.bkparking.mapper.ParkingLotMapper;
import com.zlt.bkparking.service.ParkingService;
import com.zlt.bkparking.vo.ParkingLotVO;
import com.zlt.bkparking.vo.ParkingVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class ParkingServiceImpl implements ParkingService {

    @Autowired
    private BackUserMapper backUserMapper;

    @Autowired
    private ParkingLotMapper parkingLotMapper;

    @Autowired
    private CarAddressMapper carAddressMapper;

    @Autowired
    private MyMapper myMapper;

    @Override
    public boolean setPriceByPPD(ParkingPriceDto parkingPriceDto) {
//        System.out.println(parkingPriceDto);

        CarAddressExample carAddressExample = new CarAddressExample();

        carAddressExample.createCriteria().andPlIdEqualTo(parkingPriceDto.getPlId());
        List<CarAddress> carAddresses = carAddressMapper.selectByExample(carAddressExample);
        if (carAddresses.isEmpty()){
            CarAddress carAddress = new CarAddress();
            carAddress.setPlId(parkingPriceDto.getPlId());
            carAddress.setCaPrice(parkingPriceDto.getCaPrice());
            return carAddressMapper.insertSelective(carAddress)>0;
        }else {
            CarAddress carAddress = carAddresses.get(0);
            carAddress.setCaPrice(parkingPriceDto.getCaPrice());
            return carAddressMapper.updateByPrimaryKeySelective(carAddress) > 0;
        }
    }

    @Override
    public boolean addParkingByDto(String username,ParkingDto parkingDto) {
        ParkingLot parkingLot = new ParkingLot();
        CarAddress carAddress = new CarAddress();
        ParkingLotExample parkingLotExample = new ParkingLotExample();
        parkingLotExample.or().andPlNameEqualTo(parkingDto.getPlName());
        parkingLotExample.or().andPlAddressEqualTo(parkingDto.getPlAddress());
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
//        parkingLots.forEach(p-> System.out.println(p));
        if (!parkingLots.isEmpty()){
            throw new RuntimeException("已经存在对应停车场");
        }
        BackUserExample backUserExample = new BackUserExample();
        backUserExample.createCriteria().andBuUsernameEqualTo(username);
        BackUser backUser = backUserMapper.selectByExample(backUserExample).get(0);
        BeanUtils.copyProperties(parkingDto,parkingLot);
        BeanUtils.copyProperties(parkingDto,carAddress);
        parkingLot.setBuId(backUser.getBuId());
        parkingLot.setPlState((byte)0);
        int i = parkingLotMapper.insert(parkingLot);
        parkingLotExample.clear();
        parkingLotExample.createCriteria().andPlNameEqualTo(parkingLot.getPlName());
        ParkingLot parkingLot1 = parkingLotMapper.selectByExample(parkingLotExample).get(0);
        carAddress.setPlId(parkingLot1.getPlId());
        int j = carAddressMapper.insert(carAddress);
        return i+j==2;
    }

    @Override
    public List<ParkingVo> getInfo() {
        List<ParkingVo> list = myMapper.selectAllParking();
        return list;
    }

    @Override
    public List<ParkingVo> getInfo(Integer id) {
        List<ParkingVo> list = myMapper.selectParkingByPid(id);
        return list;
    }

    @Override
    public boolean setByDto(ParkingDto parkingDto) {
        int i=0,j=0;
        ParkingLot parkingLot = new ParkingLot();
        BeanUtils.copyProperties(parkingDto,parkingLot);
        i=parkingLotMapper.updateByPrimaryKeySelective(parkingLot);
        if (parkingDto.getCaPrice()!=null||parkingDto.getCaSurplus()!=null||parkingDto.getCaTotal()!=null){
            CarAddress carAddress = new CarAddress();
            BeanUtils.copyProperties(parkingDto,carAddress);
            CarAddressExample carAddressExample = new CarAddressExample();
            carAddressExample.createCriteria().andPlIdEqualTo(carAddress.getPlId());
            List<CarAddress> carAddresses = carAddressMapper.selectByExample(carAddressExample);
            if (carAddresses.isEmpty()){
                CarAddress carAddress1 = new CarAddress();
                carAddress1.setPlId(parkingDto.getPlId());
                carAddress1.setCaTotal(parkingDto.getCaTotal());
                carAddress1.setCaPrice(parkingDto.getCaPrice());
                carAddress1.setCaSurplus(parkingDto.getCaSurplus());
                j = carAddressMapper.insertSelective(carAddress1);
            }else {
                CarAddress carAddress1 = carAddresses.get(0);
                carAddress.setCaId(carAddress1.getCaId());
                j=carAddressMapper.updateByPrimaryKeySelective(carAddress);
            }

        }
        return i+j>0;
    }

    @Override
    public boolean delByDto(Integer id) {
        ParkingLotExample parkingLotExample = new ParkingLotExample();
        parkingLotExample.createCriteria().andPlIdEqualTo(id);
        ParkingLot parkingLot = parkingLotMapper.selectByExample(parkingLotExample).get(0);
        parkingLot.setPlState((byte)1);
        System.out.println(parkingLot);
        return parkingLotMapper.updateByPrimaryKey(parkingLot)>0;
    }

    @Override
    public List<HashMap<String,Object>> getSelectParking(){
        ParkingLotExample example = new ParkingLotExample();
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(example);
        return parkingLots.stream().map(o -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("label", o.getPlName());
            map.put("value", o.getPlId());
            return map;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ParkingLotVO> ParkingLots() {
        ////////////////////等待ActiveUser//////////////////////////////
/*        ActiveUser activeUser = SecurityUtils.getSubject().getPrincipal();
        if(activeUser==null){
            throw new RuntimeException("当前未登录");
        }*/
        //test

        ParkingLotExample parkingLotExample=new ParkingLotExample();
        parkingLotExample.createCriteria().andBuIdEqualTo(1l);
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
        if(parkingLots==null){
            throw new RuntimeException("数据查询出错");
        }
        List<ParkingLotVO> parkingLotVOS=new ArrayList<>();
        for (ParkingLot parkingLot : parkingLots) {
            ParkingLotVO parkingLotVO=new ParkingLotVO();
            parkingLotVOS.add(parkingLotVO);
            BeanUtils.copyProperties(parkingLot,parkingLotVO);
        }
        return parkingLotVOS;
    }
}
